package com.e3.basdoc.employeecls.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.e3.basdoc.employeecls.dao.EmployeeClsDAO;
import com.e3.basdoc.employeecls.entity.EmployeeClsEntity;
import com.e3.basdoc.employeecls.model.EmployeeClsModel;
import com.e3.basdoc.employeecls.service.EmployeeClsCacheManager;
import com.e3.basdoc.employeecls.service.EmployeeClsService;
import com.e3.core.base.DAOHelper;
import com.e3.core.base.ServiceException;
import com.e3.core.base.SuperService;

public class EmployeeClsServiceImpl extends SuperService implements	EmployeeClsService {
	
	private EmployeeClsDAO goodsClsDAO;
	
	public EmployeeClsDAO getEmployeeClsDAO(){
		if(goodsClsDAO==null){
			goodsClsDAO = DAOHelper.createDAOInstance(EmployeeClsDAO.class);
		}
		return goodsClsDAO;
	}

	public EmployeeClsModel newEmployeeCls(EmployeeClsModel model) throws ServiceException {
		validateEmployeeCls(model);
		checkEmployeeClsExisted(model);
		
		EmployeeClsEntity entity = modelToEntity(model, null);
		getEmployeeClsDAO().persist(entity);
		
		model.setId(entity.getId());
		
		EmployeeClsCacheManager.INSTANCE.put(model);
		
		return model;
	}
	
	public EmployeeClsModel modifyEmployeeCls(EmployeeClsModel model) throws ServiceException{
		validateEmployeeCls(model);
		
		long id = model.getId();

		EmployeeClsEntity entity = getEmployeeClsDAO().getById(EmployeeClsEntity.class, id);
		if (entity == null) {
			return newEmployeeCls(model);
		}

		String oldCode = entity.getCode();
		if (!oldCode.equals(model.getCode())) {
			checkEmployeeClsExisted(model);
		}

		modelToEntity(model, entity);
		getEmployeeClsDAO().merge(entity);
		
		EmployeeClsCacheManager.INSTANCE.put(model);
		return model;
	}
	
	public void deleteEmployeeClsByIds(List<Long> ids) throws ServiceException{
		List<Long> deepIds = getDeepChildrenIds(ids);
		getEmployeeClsDAO().deleteEmployeeClsByIds(deepIds);
		
		EmployeeClsCacheManager.INSTANCE.remove(deepIds);
	}
	
	private List<Long> getDeepChildrenIds(List<Long> goodsClsIds) throws ServiceException{
		if(goodsClsIds==null||goodsClsIds.isEmpty()){
			return null;
		}
		List<Long> ids = new ArrayList<Long>();
		for (long id : goodsClsIds) {
			if(!ids.contains(id)){
				ids.add(id);
			}
		}
		List<Long> childrenIds = getEmployeeClsDAO().getChildrenIds(goodsClsIds);
		if(childrenIds!=null&&childrenIds.size()>0){
			List<Long> cIds = getDeepChildrenIds(childrenIds);
			for (Long id : cIds) {
				if(!ids.contains(id)){
					ids.add(id);
				}
			}
		}
		return ids; 
	}
	
	public void checkEmployeeClsExisted(EmployeeClsModel model) throws ServiceException {
		if (model == null) {
			return;
		}
		String code = model.getCode();
		String name = model.getName();

		StringBuffer msgs = new StringBuffer();
		long size = getEmployeeClsDAO().getSizeByCode(code);
		if(size>0){
			msgs.append("编码为'" + code+"' ");
		}
		size = getEmployeeClsDAO().getSizeByName(name);
		if (size>0) {
			msgs.append("名称为'" + name + "' ");
		}
		if(msgs.length()>0){
			msgs.append("已经存在，请设置新的编码和名称。");
			throw new ServiceException(msgs.toString());
		}
	}
	
	private void validateEmployeeCls(EmployeeClsModel model) throws ServiceException{
		String code = model.getCode();
		String name = model.getName();

		if (code == null || code.trim().length() == 0 || name == null || name.trim().length() == 0) {
			throw new ServiceException("请设置编码和名称。");
		}
	}

	private EmployeeClsEntity modelToEntity(EmployeeClsModel model, EmployeeClsEntity entity) {
		entity = ocModelToEntity(EmployeeClsEntity.class, model, entity);

		entity.setCode(model.getCode());
		entity.setName(model.getName());
		entity.setRemark(model.getRemark());
		entity.setParentId(model.getParentId());
		
		return entity;
	}

}
